#include "gps.h"

GPS::GPS(void): name(""), settings(), myStatus(GPSoff), myThread(),
    myCoord(0, 0), ready(false), dataactual(false)
{
    connect(&myThread, SIGNAL(CoordinatesChanged()), this, SLOT(SetCoordinates()));
}

GPS::~GPS()
{
    myThread.stop();
    myThread.wait();
}

bool GPS::Setup(const SerialPortSettings &aSettings, const QString &aName)
{
    if(!aName.isEmpty())
    {
        name = aName;
        settings = aSettings;
        ready = true;
    }
    else
        ready = false;
    return ready;
}

bool GPS::TurnOn(void)
{
    if(ready)
    {
        myThread.Setup(settings, name);
        myThread.start();
    }
    if(myThread.isRunning())
    {
        myStatus = GPSon;
        return true;
    }
    else
    {
        myThread.stop();
        myStatus = GPSoff;
        dataactual = false;
        return false;
    }
}

bool GPS::TurnOff(void)
{
    myThread.stop();
    myStatus = GPSoff;
    dataactual = false;
    return true;
}

ReceiverStatus GPS::GetStatus(void) const
{
    return myStatus;
}

bool GPS::GetCoordinates(GeoCoordinates &aCoord) const
{
    aCoord.SetLatitude(myCoord.GetLatitude());
    aCoord.SetLongitude(myCoord.GetLongitude());
    return dataactual;
}

void GPS::SetCoordinates(void)
{
    dataactual = myThread.GetCoordinates(myCoord);
    emit CoordinatesChanged();
}

bool GPS::GetInfo(nmeaINFO &info)
{
    myThread.GetInfo(info);
    return dataactual;
}
